package top.hkyzf.study.leetcode.array;

import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 朱峰
 * @date 2022-2-4 14:05
 */
public class 可以形成最大正方形的矩形数目_1725 {
    public int countGoodRectangles(int[][] rectangles) {
        Map<Integer, Integer> map = new HashMap<>();
        int maxKey = 0;
        for (int[] rectangle : rectangles) {
            int min = Math.min(rectangle[0], rectangle[1]);
            maxKey = Math.max(maxKey, min);
            map.put(min, map.getOrDefault(min, 0) + 1);
        }
        return map.get(maxKey);
    }

    public int countGoodRectangles1(int[][] rectangles) {
        int maxKey = 0;
        int result = 0;
        for (int[] rectangle : rectangles) {
            int min = Math.min(rectangle[0], rectangle[1]);
            if (min == maxKey) {
                result++;
            } else if (maxKey < min) {
                maxKey = min;
                result = 1;
            }
        }
        return result;
    }

    @Test
    public void testCountGoodRectangles () {
        int[][] rectangles = {{5,8},{3,9},{5,12},{16,5}};
        System.out.println(countGoodRectangles(rectangles));
    }
}
